//
//  AppDelegate.swift
//  DLSwift微博
//
//  Created by 丁浪涛 on 2016/11/7.
//  Copyright © 2016年 丁浪涛. All rights reserved.
//

import UIKit
import UserNotifications //ios 推出
import SVProgressHUD
import AFNetworking
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        //应用程序额外设置
        setupAddition()
     
        window  = UIWindow()
        window?.backgroundColor = UIColor.white
        window?.rootViewController = WBMainViewController()
        window?.makeKeyAndVisible()
        
        loadAppInfo()
        
        return true
    }


}
//设置app 额外信息
extension AppDelegate
{
    fileprivate func setupAddition(){
        //设置SVProgressHUD最小解除时间
    SVProgressHUD.setMinimumDismissTimeInterval(1)
        //2>设置网络加载指示器 顶部状态栏的 加载句话
    AFNetworkActivityIndicatorManager.shared().isEnabled = true
        
        //3>   取得用户授权显示通知【上方的提示条／声音／badgeNumber】
        //检测设备版本 10以上
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.badge,.sound,.carPlay]){ (success,error) in
                
            }
        } else {
            let notifySetting = UIUserNotificationSettings(types: [.alert,.badge,.sound ], categories: nil)
            UIApplication.shared.registerUserNotificationSettings(notifySetting)
        }
    }
}


    //从服务器加载信息
extension AppDelegate
{
        fileprivate func  loadAppInfo()
        {
            //模拟异步加载
            DispatchQueue.global().async {
                //1 url
                let url  = Bundle.main.url(forResource: "demo.json", withExtension: nil)
                let data  = NSData.init(contentsOf: url!)
                
                //取沙盒目录
                let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
                let  jsonPath = (docDir as NSString).appendingPathComponent("demo.json")
                data?.write(toFile: jsonPath, atomically: true)
                
                print("应用程序数据加载完毕 ：\(jsonPath)")
            }
    }


}
